home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example6.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  200 lines

  1. /*
  2. **    example6.c
  3. **
  4. ** This example illustrates opening a data file, inserting data
  5. ** from the file into a newly created table containing several 
  6. ** SQL Server datatypes, and updating the table using browse-mode
  7. ** techniques.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <sybfront.h>
  12. #include <sybdb.h>
  13.  
  14. #define BUFLEN 2048
  15.  
  16. /* Forward declarations of the error-handler and message-handler routines. */
  17. int           err_handler();
  18. int           msg_handler();
  19.  
  20. main()
  21. {
  22.     LOGINREC         *login;
  23.     DBPROCESS        *q_dbproc;     /* This DBPROCESS will be used to
  24.                                      * query the database.
  25.                                      */
  26.     DBPROCESS        *u_dbproc;     /* This DBPROCESS will be used to
  27.                                      * simultaneously update the database.
  28.                                      */
  29.     char             *qualptr;      /* This will point to the WHERE clause
  30.                                      * appropriate for updating q_dbproc's
  31.                                      * current data row.
  32.                                      */
  33.     RETCODE          return_code;
  34.  
  35.     DBTINYINT        age;
  36.  
  37.     char             cmdbuf[BUFLEN];
  38.     FILE             *infile;
  39.  
  40.     /* Initialize DB-Library. */
  41.     if (dbinit() == FAIL)
  42.         exit(ERREXIT);
  43.  
  44.     /* Install the user-supplied error-handling and message-handling
  45.      * routines. They are defined at the bottom of this source file.
  46.      */
  47.     dberrhandle(err_handler);
  48.     dbmsghandle(msg_handler);
  49.     
  50.     /* Allocate and initialize the LOGINREC structure to be used
  51.      * to open a connection to SQL Server.
  52.      */
  53.  
  54.     login = dblogin();
  55.     DBSETLPWD(login, "server_password");
  56.     DBSETLAPP(login, "example6");
  57.  
  58.     q_dbproc = dbopen(login, NULL);
  59.     u_dbproc = dbopen(login, NULL);
  60.  
  61.     printf("Creating the 'alltypes' table.\n");
  62.  
  63.     /* Create a table that contains several SQL Server data types. */
  64.     dbcmd(q_dbproc,"create table alltypes ");
  65.     dbcmd(q_dbproc,"(age tinyint,");
  66.     dbcmd(q_dbproc,"userid smallint,");
  67.     dbcmd(q_dbproc,"royalty int,");
  68.     dbcmd(q_dbproc,"name char(25),");
  69.     dbcmd(q_dbproc,"title_id varbinary(20),"); 
  70.     dbcmd(q_dbproc,"us_citizen bit,");
  71.     dbcmd(q_dbproc,"account float,");
  72.     dbcmd(q_dbproc,"title varchar(20),");
  73.     dbcmd(q_dbproc,"manager char(25),");
  74.     dbcmd(q_dbproc,"timestamp)");
  75.  
  76.     dbcmd(q_dbproc, "create unique index index1 on alltypes(userid)");
  77.   
  78.     dbsqlexec(q_dbproc);
  79.     while (dbresults(q_dbproc) != NO_MORE_RESULTS)
  80.         continue;
  81.  
  82.     /* Insert rows of data into the newly created table "alltypes".
  83.      * We will read in the contents of the file and form an
  84.      * INSERT statement.
  85.      */
  86.  
  87.     if ((infile=fopen("datafile","r")) == NULL)
  88.     {
  89.         printf("Unable to open file 'datafile'.\n");
  90.         exit(STDEXIT);
  91.     }
  92.  
  93.     printf("Inserting rows into the 'alltypes' table...\n");
  94.  
  95.     while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
  96.     {
  97.         dbfcmd(q_dbproc,"insert into alltypes \n");
  98.         dbfcmd(q_dbproc,"values(%s, null) \n",cmdbuf);
  99.     }
  100.  
  101.     dbsqlexec(q_dbproc);
  102.  
  103.     /* Process the results of each of the INSERT statements. */
  104.  
  105.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  106.     {
  107.         if (return_code == FAIL)
  108.             printf("One of the insert statements FAILed.\n");
  109.     }
  110.  
  111.     /* Using DB-Library's browse-mode facilities, we'll increment
  112.      * the age of every person in the table.
  113.      */
  114.  
  115.     printf("Updating rows in the 'alltypes' table...\n");
  116.  
  117.     dbcmd(q_dbproc,"select * from alltypes for browse");
  118.     dbsqlexec(q_dbproc);
  119.  
  120.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  121.     {
  122.         if (return_code == SUCCEED)
  123.         {
  124.             while (dbnextrow(q_dbproc) != NO_MORE_ROWS)
  125.             {
  126.                 age = *((DBTINYINT *)(dbdata(q_dbproc, 1)));
  127.                 qualptr = dbqual(q_dbproc, -1, "alltypes");
  128.                 dbcmd(u_dbproc, "update alltypes");
  129.                 dbfcmd
  130.                     (u_dbproc, " set age = %d %s", age+1, qualptr);
  131.                 dbsqlexec(u_dbproc);
  132.                 dbresults(u_dbproc);
  133.                 dbfreequal(qualptr);
  134.             }
  135.         }
  136.     }
  137.  
  138.     /* Now, we'll look at the updated contents of the table, to
  139.      * verify that the ages were properly incremented.
  140.      */
  141.     printf("Selecting rows from the 'alltypes' table:\n");
  142.     dbcmd(q_dbproc, "select * from alltypes");
  143.     dbsqlexec(q_dbproc);
  144.     dbresults(q_dbproc);
  145.     dbprrow(q_dbproc);
  146.  
  147.     dbexit();
  148.     exit(STDEXIT);
  149.  
  150. }
  151.  
  152. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  153. DBPROCESS       *dbproc;
  154. int             severity;
  155. int             dberr;
  156. int             oserr;
  157. char            *dberrstr;
  158. char            *oserrstr;
  159. {
  160.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  161.         return(INT_EXIT);
  162.     else 
  163.     {
  164.         printf("DB-Library error:\n\t%s\n", dberrstr);
  165.  
  166.         if (oserr != DBNOERR)
  167.             printf("Operating-system error:\n\t%s\n", oserrstr);
  168.  
  169.         return(INT_CANCEL);
  170.     }
  171. }
  172.  
  173. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  174.                 srvname, procname, line)
  175.  
  176. DBPROCESS       *dbproc;
  177. DBINT           msgno;
  178. int             msgstate;
  179. int             severity;
  180. char            *msgtext;
  181. char            *srvname;
  182. char            *procname;
  183. DBUSMALLINT     line;
  184.  
  185. {
  186.     printf ("Msg %ld, Level %d, State %d\n", 
  187.             msgno, severity, msgstate);
  188.  
  189.     if (strlen(srvname) > 0)
  190.         printf ("Server '%s', ", srvname);
  191.     if (strlen(procname) > 0)
  192.         printf ("Procedure '%s', ", procname);
  193.     if (line > 0)
  194.         printf ("Line %d", line);
  195.  
  196.     printf("\n\t%s\n", msgtext);
  197.  
  198.     return(0);
  199. }
  200.